package com.secxiot.provider.user.service.impl;

import com.secxiot.provider.task.entity.SosDeptFacilityDo;
import com.secxiot.provider.task.vo.SosDeptFacilityVo;
import com.secxiot.provider.user.dao.*;
import com.secxiot.provider.user.dto.DeptDto;
import com.secxiot.provider.user.dto.UserDeptDto;
import com.secxiot.provider.user.entity.DeptUsersDo;
import com.secxiot.provider.user.entity.UserDo;
import com.secxiot.provider.user.service.DeptService;
import com.secxiot.provider.user.vo.DeptVo;
import com.secxiot.provider.user.vo.HospitalVo;
import com.secxiot.provider.user.vo.UserSimpleVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;
    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private FacilityDeptSosDao facilityDeptSosDao;

    @Autowired
    private UserMapper userMapper;

    @Override
    public DeptDto getDeptByUserId(Long userId) {

        return deptDao.getDeptByUserId(userId);
    }

    @Override
    public DeptDto getDeptByDeptId(Long deptId) {

        return deptDao.getDeptByDeptId(deptId);
    }

    @Override
    public List<DeptDto> listSubdeptByDeptId(Long deptId) {
        return deptDao.listSubdeptByDeptId(deptId);
    }

    @Override
    public List<DeptUsersDo> DeptUsersDoById(Long id) {
        return deptDao.deptUsersDoById(id);
    }

    @Override
    public List<HospitalVo> hospitalListByUser(Long userId, BigDecimal longitude, BigDecimal latitude, String wxAppId) {
        return hospitalDao.selectHospitalListByUser(longitude, latitude, wxAppId);
    }

    @Override
    public HospitalVo getNearestHospital(BigDecimal longitude, BigDecimal latitude, Long hospitalId) {
        return hospitalDao.selectNearestHospital(longitude, latitude, hospitalId);
    }

    @Override
    public HospitalVo getHospitalById(Long id) {
        return hospitalDao.getHospitalById(id);
    }

    @Override
    public HospitalVo getHospitalByDeptId(Long deptId) {
        return hospitalDao.getHospitalByDeptId(deptId);
    }

    @Override
    public HospitalVo getHospitalDistanceByDeptId(BigDecimal longitude, BigDecimal latitude, Long deptId) {
        return hospitalDao.getHospitalDistanceByDeptId(longitude, latitude, deptId);
    }

    @Override
    public List<HospitalVo> selectHospital() {
        return hospitalDao.selectHospital();
    }


    /**
     * 获取医院设备
     *
     * @param deptId     部门id
     * @param hospitalId 医院ID
     * @return
     */
    @Override
    public List<SosDeptFacilityVo> SosFacility(@Param("deptId") Long deptId, @Param("hospitalId") Long hospitalId) {
        List<SosDeptFacilityVo> list = new ArrayList<>();
        SosDeptFacilityDo facilityDeptSosDo = new SosDeptFacilityDo();
        facilityDeptSosDo.setDeptId(deptId);
        facilityDeptSosDo.setHospitalId(hospitalId);//医院id
        List<SosDeptFacilityDo> facilityDeptSosDos = facilityDeptSosDao.SelectFacilityDeptSosDo(facilityDeptSosDo);
        if (!CollectionUtils.isEmpty(facilityDeptSosDos)) {
            return list;
        }

        for (SosDeptFacilityDo deptSosDo : facilityDeptSosDos) {
            SosDeptFacilityVo sosDeptFacilityVo = new SosDeptFacilityVo();
            sosDeptFacilityVo.setId(deptSosDo.getId());
            sosDeptFacilityVo.setFacilityName(deptSosDo.getFacilityName());
            list.add(sosDeptFacilityVo);
        }
        return list;
    }

    @Override
    public List<UserSimpleVo> listSubManagers(Long deptId) {
        return deptDao.selectSubManagersByDeptId(deptId);
    }

    @Override
    public List<UserDeptDto> listSupperDeptByUserId(Long userId) {
        UserDo user = userMapper.selectByPrimaryKey(userId);
        String type = user.getType();
        return deptDao.selectSupperDeptsByUserId(userId, type);
    }

    @Override
    public List<DeptDto> listToLeafByDeptId(Long deptId) {
        DeptDto dept = this.getDeptByDeptId(deptId);
        StringBuilder sb = new StringBuilder(dept.getSupperIds());
        sb.append("-").append(deptId);
        List<DeptDto> list = deptDao.listToLeafByDeptId(sb.toString(), sb.append("-").append("%").toString());
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
        }
        list.add(0, dept);
        return list;
    }

    /**
     * 获取parentId下的部门列表
     *
     * @param parentId 部门父ID
     * @return
     */
    @Override
    public List<DeptVo> listDeptByParentId(Long parentId) {
        return deptDao.getDeptListByParentId(parentId);
    }
}
